package org.feng.common;

import lombok.extern.slf4j.Slf4j;
import org.feng.businessbean.MyBean;
import org.feng.businessbean.MyBean1;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 在应用启动之后，输出控制台数据
 *
 * @version V1.0
 * @author: fengjinsong
 * @date: 2022年07月01日 15时26分
 */
@Component
@Lazy
@Order(1)
@Slf4j
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) {
        final Set<String> beanNames = CustomBeanDefinitionScanner.BEAN_NAMES;
        for (String beanName : beanNames) {
            final Object bean = BeanFactoryAwareImpl.getBean(beanName);
            if (bean instanceof MyBean1) {
                MyBean1 bean1 = (MyBean1) bean;
                bean1.setName("12345678910");
                // 测试切面
                bean1.doSome();
            }
            log.info("注册bean {} 成功！ --beanName={} --hashCode={}", bean, beanName, bean.hashCode());

        }
        final Object bean = BeanFactoryAwareImpl.getBean("one-2_two-2_BeanBusiness");
        log.info("mybean2 =====> {} {}", bean, bean.hashCode());
        log.info("注册结束，共注册 {} 个 Bean", CustomBeanDefinitionScanner.BEAN_NAMES.size());
        CustomBeanDefinitionScanner.BEAN_NAMES.clear();

        // 手动获取自定义的bean
        BusinessOneEnum one = BusinessOneEnum.ONE_TEST1;
        BusinessTwoEnum two = BusinessTwoEnum.TWO_TEST1;
        final Object businessBean = BeanFactoryAwareImpl.getBusinessBean(one, two);
        MyBean myBean = (MyBean) businessBean;
        log.info("通过 one={} two={} 获取到 bean={} hashCode={}", myBean.getOne(), myBean.getTwo(), businessBean, businessBean.hashCode());
    }
}
